import List from "../../../components/List";
import PopupBase from "../../../components/popups/PopupBase";
import EventManager from "../../../core/EventManager";
import PopupManager from "../../../core/PopupManager";
import { BattleTest } from "../../data/BattleTest";
import ItemHeroIcon from "../items/ItemHeroIcon";
import ItemHeroPos from "../items/ItemHeroPos";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PopupFormation extends PopupBase<any> {

    @property({ type: [ItemHeroPos] })
    heroPosCtrls: Array<ItemHeroPos> = [];

    //水平列表
    @property(List)
    listH: List = null;

    data: Array<number> = [];

    selectedList: Array<number> = [];

    protected onLoad(): void {
        this.data = [];
        this.selectedList = [];
    }

    init(options: any) {

    }

    onShow() {
        this.listH.node.active = true;
        this.data = BattleTest.ins().heros;
        this.listH.numItems = this.data.length;
        console.log('length>>>>>', this.data.length)
    }

    initHeroPos(heroId) {
        for (let i = 0; i < this.heroPosCtrls.length; i++) {
            let itemCtrl = this.heroPosCtrls[i];
            if (itemCtrl.heroId == 0) {
                itemCtrl.init(heroId);
                break;
            }
        }
    }

    clearHeroPos(heroId) {
        for (let i = 0; i < this.heroPosCtrls.length; i++) {
            let itemCtrl = this.heroPosCtrls[i];
            if (itemCtrl.heroId == heroId) {
                itemCtrl.hideHeroNode();
                break;
            }
        }
    }

    onListHRender(item: cc.Node, idx: number) {
        // console.log('H--idx>>>>>',idx)
        let itemCtrl = item.getComponent(ItemHeroIcon);
        if (itemCtrl) {
            itemCtrl.init(this.data[idx]);
        }
    }

    //当列表项被选择...
    onListSelected(item: any, selectedId: number, lastSelectedId: number, val: number) {
        if (!item)
            return;
        let list: List = item.getComponent(ItemHeroIcon).list;
        // let str: string = '当前操作List为：' + list.node.name + '，当前选择的是：' + selectedId + '，上一次选择的是：' + lastSelectedId;
        if (list.selectedMode == 2) { //如果是多选模式
            // str += '，当前值为：' + val;
            if (val) {
                this.initHeroPos(this.data[selectedId]);
                this.selectedList.push(selectedId);
            } else {
                let idx = this.selectedList.indexOf(selectedId);
                if (idx != -1) {
                    this.selectedList.splice(idx, 1);
                    this.clearHeroPos(this.data[selectedId]);
                }
            }
        }
        // cc.log(str);
        // console.log(this.selectedList);
        if (this.selectedList.length > 5) {
            PopupManager.show('prefabs/PopBox', {
                title: '提示',
                content: '选择数量超过上限',
                confirmCallback: () => {
                }
            },{immediately:true});
        }
    }

    onClose() {
        this.hide();
    }

    onSave() {
        BattleTest.ins().ownerMap = {}
        for (let i = 0; i < this.selectedList.length; i++) {
            const element = this.selectedList[i];
            BattleTest.ins().ownerMap[i + 1] = this.data[element];
        }
        EventManager.emit("InitBattleUIHeros");
    }

    onSort() {

    }
}
